﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using BusinessManagementStudio.Models.BusinessObject.Entities;
using BusinessManagementStudio.Models.BusinessObject.Services;
using BusinessManagementStudio.ViewModel;

namespace BusinessManagementStudio.Controllers
{
    public class MaintenanceController : Controller
    {
        private CompanyService cs = new CompanyService();
        private FMService fms = new FMService();
        private ClientService clnts = new ClientService();
        private ContractorService conts = new ContractorService();
        private ContractorAssignmentService contAssigns = new ContractorAssignmentService();
        //
        // GET: /Maintenance/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult MultipleCommandForAdmin(String command, String CompanyName, String CurrCompanyID, String FMCode, String CurrFMID,
            String ComName, String FacMCode, String BillName, String BillAddress1, String BillAddress2, String BillAddress3, String BillAddress4,
            String TelephoneNo, String FaxNo, String AttnTo, String ContractorCode, String CurrContractorID, String ContractorName)
        {
            if (command == "Create New Client")
                return RedirectToAction("ClientCreation", "Maintenance");
            else if (command == "Create New FM")
                return RedirectToAction("FMCreation", "Maintenance");
            else if (command == "Create New Company")
                return RedirectToAction("CompanyCreation", "Maintenance");
            else if (command == "Create New Contractor")
                return RedirectToAction("ContractorCreation", "Maintenance");
            else if (command == "Contractor Assignment")
                return RedirectToAction("ContractorManualAssignment", "Maintenance");
            else if (command == "Create A New Order")
                return RedirectToAction("Index", "Order");
            else if (command == "Cancel" || command == "Back")
            {
                if (TempData["UserObj"] != null)
                {
                    User CurrentUser = (User)TempData["UserObj"];

                    if (CurrentUser.UserType == "Administrator")
                        return RedirectToAction("Admin", "User");
                    else if (CurrentUser.UserType == "User")
                        return RedirectToAction("NonAdmin", "User");
                }
            }
            else if (command == "Quotation & Invoice Enquiry")
                return RedirectToAction("Index", "QuotationAndInvoice");
            else if (command == "CreateCompany")
            {
                #region Company Creation
                if (CompanyName != "")
                {
                    Company cpy = new Company();
                    cpy.CompanyID = int.Parse(CurrCompanyID) + 1;
                    cpy.CompanyName = CompanyName;

                    cs.Create(cpy);

                    return RedirectToAction("Successful", "Home", new { ComName = cpy.CompanyName, FMName = "" });
                }
                #endregion
            }
            else if (command == "CreateFM")
            {
                #region FM Creation
                if (FMCode != "")
                {
                    FM fm = new FM();
                    fm.FMID = int.Parse(CurrFMID) + 1;
                    fm.FMName = FMCode;

                    fms.Create(fm);

                    return RedirectToAction("Successful", "Home", new { ComName = "", FMName = fm.FMName });
                }
                #endregion
            }
            else if (command == "CreateContractor")
            {
                #region Contractor Creation
                if (ContractorCode != "" && ContractorName != "")
                {
                    Contractor contractor = new Contractor();

                    contractor.ContractorID = int.Parse(CurrContractorID) + 1;
                    contractor.ContractorCode = ContractorCode;
                    contractor.ContractorName = ContractorName;
                    contractor.IsValid = true;

                    conts.Create(contractor);

                    return RedirectToAction("Successful", "Home", new { ComName = "", FMName = "", ClientName = "", ContractorName = contractor.ContractorName });
                }
                #endregion
            }
            else if (command == "Reports")
            {
                return RedirectToAction("Reports", "Report");
            }
            else if (command == "CreateClient")
            {
                #region Client Creation
                if (ComName != "" && FacMCode != "" && BillName != "" && BillAddress1 != "")
                {
                    Client client = new Client();
                    client.CompanyName = ComName;
                    client.FMCode = FacMCode;
                    client.BillingName = BillName;
                    client.BillingAddress1 = BillAddress1;
                    client.BillingAddress2 = BillAddress2;
                    client.BillingAddress3 = BillAddress3;
                    client.BillingAddress4 = BillAddress4;
                    client.TelephoneNo = TelephoneNo;
                    client.FaxNo = FaxNo;
                    client.AttentionTo = AttnTo;

                    clnts.Create(client);

                    return RedirectToAction("Successful", "Home", new { ComName = "", FMName = "", ClientName = client.AttentionTo });
                }
                #endregion
            }
            else if (command == "Reset Password")
                return RedirectToAction("PasswordReset", "User");
            else if (command == "Logout")
            {
                User u = (User)TempData["UserObj"];
                String s1 = (String)TempData["CurrentViewName"];
                String s2 = (String)TempData["CurrentViewName_Quotation"];
                CompanyOrder co = (CompanyOrder)TempData["CompanyOrder"];
                CompanyRealOrder cro1 = (CompanyRealOrder)TempData["Quotation_Invoice_Model"];
                OrderInvoiceQuotation jio = (OrderInvoiceQuotation)TempData["JLLOrderInvoice"];
                OrderInvoiceQuotation Jpo = (OrderInvoiceQuotation)TempData["JLLPOInvoice"];
                return RedirectToAction("Index", "Home");
            }

            return View();
        }

        [HttpPost]
        public ActionResult NewOrderCreation(String UserType)
        {
            if (UserType == "Admin")
                return RedirectToAction("Index", "Order");
            else if(UserType == "NonAdmin")
                return RedirectToAction("Index", "Order");

            return View();
        }

        [HttpPost]
        public ActionResult MultipleCommandForNonAdmin(String command)
        {
            if (command == "Cancel")
                return RedirectToAction("NonAdmin", "User");
            else if (command == "Create A New Order")
                return RedirectToAction("Index", "Order");
            else if (command == "Quotation & Invoice Enquiry")
                return RedirectToAction("Index", "QuotationAndInvoice");
            else if (command == "Contractor Assignment")
                return RedirectToAction("ContractorManualAssignment", "Maintenance");
            else if (command == "Reset Password")
                return RedirectToAction("PasswordReset", "User");
            else if (command == "Logout")
                return RedirectToAction("Index", "Home");

            return View();
        }

        public ActionResult ClientCreation()
        {
            CompanyFMCode companyFMCodeViewModel = new CompanyFMCode();
            companyFMCodeViewModel.company = new Company();
            companyFMCodeViewModel.company.CompanyNameList = cs.GetCompanyNameList();
            companyFMCodeViewModel.fm = new FM();

            List<SelectListItem> FMsList = fms.GetFMList();
            SelectListItem tempItem = new SelectListItem();
            tempItem.Text = "NIL";
            tempItem.Value = "NIL";
            FMsList.Add(tempItem);
            companyFMCodeViewModel.fm.FMNameList = FMsList;

            return View(companyFMCodeViewModel);
        }

        public ActionResult CompanyCreation()
        {
            ComCompanyCreation companyViewModel = new ComCompanyCreation(cs.GetAllCompanies());

            return View(companyViewModel);
        }

        public ActionResult FMCreation()
        {
            FManagementCreation fmViewModel = new FManagementCreation(fms.GetAllFMs());

            return View(fmViewModel);
        }

        public ActionResult ContractorCreation()
        {
            ContContractorCreation contractorViewModel = new ContContractorCreation(conts.GetAllContractors());

            return View(contractorViewModel);
        }

        public ActionResult ContractorManualAssignment()
        {
            CompanyFMContractor comfmcontractorViewModel = new CompanyFMContractor();
            comfmcontractorViewModel.fm = new FM();
            List<SelectListItem> FMsList = fms.GetFMList();
            SelectListItem tempItem = new SelectListItem();
            tempItem.Text = "NIL";
            tempItem.Value = "NIL";
            FMsList.Add(tempItem);
            comfmcontractorViewModel.fm.FMNameList = FMsList;
            comfmcontractorViewModel.company = new Company();
            comfmcontractorViewModel.company.CompanyNameList = cs.GetCompanyNameList();
            comfmcontractorViewModel.contractor = new Contractor();
            comfmcontractorViewModel.contractor.ContractorNameList = conts.GetContractorNameList();

            return View(comfmcontractorViewModel);
        }

        [HttpPost]
        public ActionResult ContractorAssignmentConfirmation(FormCollection collection, String command)
        {
            if (command == "Cancel")
            {
                if (TempData["UserObj"] != null)
                {
                    User CurrentUser = (User)TempData["UserObj"];

                    if (CurrentUser.UserType == "Administrator")
                        return RedirectToAction("Admin", "User");
                    else if (CurrentUser.UserType == "User")
                        return RedirectToAction("NonAdmin", "User");
                }
            }
            else if (command == "ConfirmAssignment")
            {
                ContractorAssignment contAssignment = new ContractorAssignment();

                var selectedCompanyID = collection["CompanyName_Assignment"];
                Company selectedCompany = cs.GetCompanyByCompanyID(int.Parse(selectedCompanyID));
                var selectedFMCode = collection["FMCode_Assignment"];
                var QuotationNo = collection["QuotationNo_Assignment"];
                var selectedContractorCode = collection["ContractorName_Assignment"];
                var contractorCurrency = collection["Contractor_Currency"];
                var contractorAmount = collection["Contractor_Amount"];
                Contractor selectedContractor = conts.GetContractorByContractorCode(selectedContractorCode);

                contAssignment.CompanyNo = int.Parse(selectedCompanyID);
                contAssignment.CompanyName = selectedCompany.CompanyName;
                contAssignment.FMCode = selectedFMCode;
                contAssignment.QuotationNo = QuotationNo;
                contAssignment.ContractorCode = selectedContractorCode;
                contAssignment.ContractorName = selectedContractor.ContractorName;
                contAssignment.AssignmentDate = DateTime.Now;
                contAssignment.ContractorCurrency = contractorCurrency;
                contAssignment.ContractorAmount = decimal.Parse(contractorAmount);

                contAssigns.Create(contAssignment);

                return RedirectToAction("Successful", "Home", new
                {
                    ComName = "",
                    FMName = "",
                    ClientName = "",
                    ContractorName = selectedContractor.ContractorName
                    , ComID = selectedCompanyID, QuoteNo = QuotationNo 
                });
            }

            return View();
        }
    }
}
